﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace GameLib {
    class ModDependency : IModDependency {
        public string FilePattern {
            get;
            set;
        }

        public string Message {
            get;
            set;
        }

        public int Priority {
            get;
            set;
        }

        public bool Obsolete {
            get;
            set;
        }

        List<IMod> IModConflict.Match(IModList mods) {
            List<IMod> result = new List<IMod>();
            foreach (IMod mod in mods) {
                if (Matches(mod)) {
                    result.Add(mod);
                }
            }
            return result;
        }

        public bool Matches(IModList mods) {
            foreach (IMod mod in mods) {
                if (Matches(mod)) {
                    return true;
                }
            }
            return false;
        }

        public bool Matches(IMod mod) {
            Regex regex = new Regex(FilePattern);
            String modFileName = Path.GetFileName(mod.File);
            return regex.IsMatch(FilePattern);

        }

        public IModDependency Clone() {
            BinaryFormatter bf = new BinaryFormatter();
            MemoryStream ms = new MemoryStream();
            bf.Serialize(ms, this);
            ms.Flush();
            ms.Position = 0;
            return (IModDependency) bf.Deserialize(ms);
        }
    }
}
